package com.tomshushu.doc.freedoc.modular.tmp.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tomshushu.doc.freedoc.modular.team.entity.TeamEntity;
import com.tomshushu.doc.freedoc.modular.tmp.entity.TmpEntity;
import com.tomshushu.doc.freedoc.modular.tmp.mapper.TmpMapper;
import com.tomshushu.doc.freedoc.modular.tmp.model.param.AddTmpParam;
import com.tomshushu.doc.freedoc.modular.tmp.model.param.DeleteTmpParam;
import com.tomshushu.doc.freedoc.modular.tmp.model.param.EditTemParam;
import com.tomshushu.doc.freedoc.modular.tmp.model.param.ListTmpParam;
import com.tomshushu.doc.freedoc.modular.tmp.service.TmpService;
import com.tomshushu.doc.freedoc.modular.user.entity.UserEntity;
import com.tomshushu.doc.freedoc.modular.user.service.UserService;
import com.tomshushu.doc.freedoc.modular.version.entity.VersionEntity;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: zhouhong
 * @date: 2025-06-10 17:26
 * @version: 1.0
 */
@Service
public class TmpServiceImpl  extends ServiceImpl<TmpMapper, TmpEntity> implements TmpService {
    @Resource
    private UserService userService;
    @Override
    public void addTmp(AddTmpParam addTmpParam) {
        String userId = StpUtil.getLoginIdAsString();
        TmpEntity tmpEntity = new TmpEntity();
        BeanUtils.copyProperties(addTmpParam, tmpEntity);
        tmpEntity.setCreateUser(userId);
        save(tmpEntity);
    }

    @Override
    public List<TmpEntity> listTmp(ListTmpParam param) {
        LambdaQueryWrapper<TmpEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(param.getTeamId())) {
            queryWrapper.eq(TmpEntity::getTeamId, param.getTeamId());
        }
        if (ObjectUtil.isNotNull(param.getProjectId())) {
            queryWrapper.eq(TmpEntity::getProjectId, param.getProjectId());
        }
        if (ObjectUtil.isNotNull(param.getType())) {
            queryWrapper.eq(TmpEntity::getType, param.getType());
        }
        if (ObjectUtil.isNotNull(param.getCreateUser())) {
            queryWrapper.eq(TmpEntity::getCreateUser, param.getCreateUser());
        }
        queryWrapper.orderByDesc(TmpEntity::getCreateTime);

        List<TmpEntity> list = this.list(queryWrapper);
        list.forEach(item -> {
            UserEntity user = userService.getById(item.getCreateUser());
            item.setCreateUser(user.getUserName());
        });

        return list;
    }

    @Override
    public void deleteTmp(DeleteTmpParam param) {
        TmpEntity tmpEntity = this.getById(param.getTmpId());
        if (ObjectUtil.isNotNull(tmpEntity)) {
            this.removeById(param.getTmpId());
        }
    }

    @Override
    public void editTmp(EditTemParam param) {
        TmpEntity tmpEntity = this.getById(param.getTmpId());
        BeanUtils.copyProperties(param, tmpEntity);
        this.updateById(tmpEntity);
    }

}